//
//  MovieViewController.m
//  快乐一点通
//
//  Created by cjp on 16/7/16.
//  Copyright © 2016年 cjp. All rights reserved.
//

#import "MovieViewController.h"
#import "MovieTableViewCell.h"
#import "MovieModel.h"

#import <AFNetworking.h>
#import <MJRefresh.h>
#import <MJExtension.h>

@interface MovieViewController ()

@property(nonatomic,strong)NSMutableArray *movieArray;

@property(nonatomic,strong)MovieModel *movieModel;

/** 上一次的请求参数 */
@property(nonatomic,strong)NSDictionary *params;

@end

static NSString *const MovieCellId = @"movie";

@implementation MovieViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self setupView];

    // 添加刷新控件
    [self setupRefresh];
    
}
- (void)setupView{
    self.navigationItem.title = @"影视资讯";
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([MovieTableViewCell class]) bundle:nil] forCellReuseIdentifier:MovieCellId];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

}

- (void)setupRefresh{
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewMovie)];
    // 自动改变透明度
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
    [self.tableView.mj_header beginRefreshing];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreMovie)];
    
    
}

- (void)loadNewMovie{
    
    // 结束上拉
    [self.tableView.mj_footer endRefreshing];
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];

    self.params = params;
    
    [[AFHTTPSessionManager manager]GET:MovieURL parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *responseObject) {
        
        if (self.params != params) return ;
        self.movieArray = [MovieModel mj_objectArrayWithKeyValuesArray:responseObject[@"videos"]];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
            // 结束刷新
            [self.tableView.mj_header endRefreshing];

        });
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (self.params != params) return ;
        // 结束刷新
        [self.tableView.mj_header endRefreshing];
    }];
    
}

- (void)loadMoreMovie{
    
    // 结束上拉
    [self.tableView.mj_header endRefreshing];
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    
    self.params = params;
    
    [[AFHTTPSessionManager manager]GET:MovieURL parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *responseObject) {
        
//        if (self.params != params) return ;
        self.movieArray = [MovieModel mj_objectArrayWithKeyValuesArray:responseObject[@"videos"]];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
            // 结束刷新
            [self.tableView.mj_footer endRefreshing];
            
        });
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        if (self.params != params) return ;
        // 结束刷新
        [self.tableView.mj_footer endRefreshing];
    }];
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    self.tableView.mj_footer.hidden = (self.movieArray.count == 0);
    return self.movieArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MovieTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MovieCellId forIndexPath:indexPath];
    
    cell.movie = self.movieArray[indexPath.row];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    _movieModel = self.movieArray[indexPath.row];

    return _movieModel.cellHeight;
}



@end
